import plugin from '../../lib/plugins/plugin.js'
import {
    segment
}
    from 'oicq'
import fetch from "node-fetch";

let url = ""
let number = 1

export class englishZcx1013 extends plugin {
    constructor() {
        super({
            /** 功能名称 */
            name: 'zqm随机单词',
            /** 功能描述 */
            dsc: '',
            event: 'message',
            priority: 1145,
            rule: [
                {
                    reg: '^#单词.*$',
                    fnc: 'zqmNb'
                }
                , {
                    reg: '^#设置单词随机数量(.*)',
                    fnc: 'setNumber'
                }
            ]

        })
    }

    async setNumber(e) {

        if (e.msg.includes('#设置单词随机数量')) {
            let keyword = e.msg.replace("#设置单词随机数量", "");
            number = Number(keyword)
            e.reply('当前返回' + keyword + '个单词')

        }
    }


    async zqmNb(e) {
        if (e.isGroup) {
            let img = []
            url = `http://150.158.1.173:8080/eng/getEng?count=${number}`;

            let response = ""; //调用接口获取数据
            let res = ""; //结果json字符串转对象
            let englishList = [];
            let englishTranslate = [];
            try {
                response = await fetch(url);
                res = await response.json();
                console.log(res)
                for (let i = 0; i < res.length; i++) {
                    englishList[i] = res[i].word;
                    englishTranslate[i] = res[i].translate;
                }

            } catch {
                return
            }
            if (res == "") {
                e.reply('接口调用失败')
            }
            //发送消息
            let text = "";
            let translateText = "";
            for (let englishListElement of englishList) {
                text = text + "单词:" + englishListElement + "\n";
            }
            for (let i = 0; i < englishTranslate.length; i++) {
                translateText = translateText + englishList[i] + "：的解释为" + "\t" + englishTranslate[i] + "\n";
            }
            await e.reply(text);
            setTimeout(function () {
                e.reply(translateText);
            }, 5000);

        }
    }

}